package com.wing.bill.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.bill.dao.PurchaseBillDao;
import com.wing.bill.entity.PurchaseBill;
import com.wing.bill.entity.PurchaseBillReceipt;
import com.wing.bill.entity.PurchaseBillSettle;
import com.wing.bill.entity.vo.BillSignVO;
import com.wing.bill.entity.vo.BillTotalVO;
import com.wing.common.exception.BusinessException;
import com.wing.product.model.response.ProductItemVO;
import com.wing.product.service.DynamicService;
import com.wing.purchase.model.entity.PurchaseSignFor;
import com.wing.purchase.model.response.PurchaseSignForVO;
import com.wing.purchase.service.PurchaseSignForService;
import com.wing.purchase.service.SupplierInfoService;
import com.wing.web.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 采购账单
 *
 * @author xiayang
 */
@Service
@Slf4j
public class PurchaseBillServiceImpl extends ServiceImpl<PurchaseBillDao, PurchaseBill> implements PurchaseBillService {

    @Resource
    DynamicService dynamicService;

    @Resource
    PurchaseBillSettleService purchaseBillSettleService;

    @Resource
    PurchaseBillReceiptService purchaseBillReceiptService;

    @Resource
    PurchaseSignForService purchaseSignForService;

    @Resource
    SupplierInfoService supplierInfoService;

    @Override
    @Transactional(readOnly = false)
    public void add(PurchaseBill purchaseBill) {
        if (StringUtils.isBlank(purchaseBill.getBillStartTime())) {
            throw new BusinessException("账单开始时间不能为空");
        }
        if (StringUtils.isBlank(purchaseBill.getBillEndTime())) {
            throw new BusinessException("账单结束时间不能为空");
        }
        //账单统计列表
        Map<String, Object> params = new HashMap<>();
        params.put("billStartTime", purchaseBill.getBillStartTime());
        params.put("billEndTime", purchaseBill.getBillEndTime());
        List<BillTotalVO> billTotalList = this.baseMapper.billTotalList(params);
        //账单签收列表
        List<BillSignVO> billSignList = this.baseMapper.billSignList(params);

        for (BillTotalVO billTotal : billTotalList) {
            PurchaseBill purchaseBill0 = new PurchaseBill();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
            String code = dynamicService.getMaxCode("code", "pur_purchase_bill");
            String day = simpleDateFormat.format(new Date());
            purchaseBill0.setCode("ZD" + day + code);
            purchaseBill0.setSupplierId(billTotal.getSupplierId());
            purchaseBill0.setBillStartTime(purchaseBill.getBillStartTime());
            purchaseBill0.setBillEndTime(purchaseBill.getBillEndTime());
            purchaseBill0.setSignOrderNum(billTotal.getSignNum());
            purchaseBill0.setPurchaseOrderNum(billTotal.getPurchaseOrderNum());
            purchaseBill0.setBillSum(billTotal.getBillSum());
            purchaseBill0.setSettleStatus("1");// 结算状态
            purchaseBill0.setReceiptStatus("1"); //发票状态
            purchaseBill0.setCreator(JwtUtils.getLoginUserId());
            purchaseBill0.setCreatorName(JwtUtils.getLoginUsername());
            purchaseBill0.setCreateTime(LocalDateTime.now());
            this.baseMapper.insert(purchaseBill0);
            //绑定签收信息
            List<BillSignVO> signList = billSignList.stream().filter(t -> t.getSupplierId().longValue() == billTotal.getSupplierId().longValue()).collect(Collectors.toList());
            for (BillSignVO billSign : signList) {
                PurchaseSignFor signFor = purchaseSignForService.getById(billSign.getSignId());
                signFor.setBillId(purchaseBill0.getId());
                purchaseSignForService.updateById(signFor);
            }
        }
    }

    @Override
    public List<PurchaseBill> list(Map<String, Object> params) {
        //结算记录列表
        Map<String, Object> settleMap = new HashMap<>();
        List<PurchaseBillSettle> settleList = purchaseBillSettleService.list(settleMap);
        //开票记录列表
        Map<String, Object> receiptMap = new HashMap<>();
        List<PurchaseBillReceipt> receiptList = purchaseBillReceiptService.list(receiptMap);
        //账单列表
        List<PurchaseBill> list = baseMapper.billList(params);
        for (PurchaseBill purchaseBill : list) {
            purchaseBill.setSettleList(settleList.stream().filter(t -> t.getBillId().longValue() == purchaseBill.getId().longValue()).collect(Collectors.toList()));
            purchaseBill.setReceiptList(receiptList.stream().filter(t -> t.getBillId().longValue() == purchaseBill.getId().longValue()).collect(Collectors.toList()));
        }
        return list;
    }

    @Override
    public Page<PurchaseBill> pageList(Page page, Map<String, Object> params) {
        //结算记录列表
        Map<String, Object> settleMap = new HashMap<>();
        List<PurchaseBillSettle> settleList = purchaseBillSettleService.list(settleMap);
        //开票记录列表
        Map<String, Object> receiptMap = new HashMap<>();
        List<PurchaseBillReceipt> receiptList = purchaseBillReceiptService.list(receiptMap);
        //签收记录列表
        Map<String, Object> signMap = new HashMap<>();
        List<PurchaseSignForVO> purchaseSignForList = purchaseSignForService.list4app(signMap);
        //账单列表
        List<PurchaseBill> list = baseMapper.billList(page, params);
        for (PurchaseBill purchaseBill : list) {
            purchaseBill.setSettleList(settleList.stream().filter(t -> t.getBillId().longValue() == purchaseBill.getId().longValue()).collect(Collectors.toList()));
            purchaseBill.setReceiptList(receiptList.stream().filter(t -> t.getBillId().longValue() == purchaseBill.getId().longValue()).collect(Collectors.toList()));
            purchaseBill.setSignList(purchaseSignForList.stream().filter(t -> t.getBillId() != null && t.getBillId().longValue() == purchaseBill.getId().longValue()).collect(Collectors.toList()));
            purchaseBill.setSupplierInfo(supplierInfoService.get4appById(purchaseBill.getSupplierId()));
        }
        page.setRecords(list);
        return page;
    }

    @Override
    public PurchaseBill get(Long id) {
        return this.baseMapper.get(id);
    }
}
